﻿
using UnityEngine;
using BridgeShips.AI;
using Framework;
using Framework.FSM;
using BridgeShips.FSM;

namespace BridgeShips
{
    public class EntityGlowingControl : IControl
    {

        private uint uID;
        public AIShip m_aiShip = null;
        public shaderGlow m_ShipGlowing = null;
        private bool m_GlowingOpen = false;

        public enum GlowingState
        {
            Glowing_None,    //NULL
            Glowing_Open,    //red _Open
            Glowing_Stop     //green _Stop
        }

        public void Init(EntityBase entity)
        {
            VehicleEntity shipeEntity = entity as VehicleEntity;
            if (shipeEntity != null)
            {
                uID = shipeEntity.GetID();
            }
        }

        public void InitDown()
        {
            ChangeGlowingState(false);
        }

        public void PostInit()
        {
            var shipeEntity = GameEntry.Instance.EntityManager.Get(uID);
            if (shipeEntity != null)
            {
                m_aiShip = shipeEntity.GetAI();
                m_ShipGlowing = shipeEntity.Get().GetComponentInChildren<shaderGlow>();
                if (m_ShipGlowing != null)
                {
                    m_ShipGlowing.enabled = false;
                }
            }
        }

        public void ChangeGlowingState(bool bOpen)
        {
            if (m_ShipGlowing == null)
                return;
            if (m_GlowingOpen == bOpen && m_ShipGlowing.GetHighlighted() == bOpen)
                return;

            if (!bOpen)
            {
                if (m_ShipGlowing.GetHighlighted())
                {
                    m_ShipGlowing.hideFlags = HideFlags.HideAndDontSave;
                    m_ShipGlowing.lightOff();
                }
                else
                    return;
            }
            else
                m_ShipGlowing.lightOn();

            m_GlowingOpen = bOpen;
        }
        public void Release()
        {
            //Debug.Log( "MoveManagerTest Release" );
        }

        public void Update(float frameTime, float realTime)
        {

            if (m_aiShip == null || m_aiShip.Settings == null)
                return;

            var e = GameEntry.Instance.EntityManager.Get(uID);
            var briShip = e.Get().GetComponent<AIEventHandler>();
            if (briShip == null)
                return;

            uint shipID = m_aiShip.Settings.Detection.GetEnity().GetID();
            IFsmState fState = null;
            bool beFar = TransferOfAxes.Instance.linkedEachOtherFarfromSetPoint(e.Get().transform.position);
            if (beFar || !GameEntry.Instance.FsmManager.GetOpenAuxillaryConnection())
            {
                fState = GameEntry.Instance.VirtualRManager.GetFsmHalfWayStateByShipID(shipID);
  
                if (m_aiShip.Settings.Movement.MovementState == ET.AIMovementState.AIDriving &&
                   fState.ToString() == "BridgeShips.FSM.VHalfWayPosCorrectState")
                {
                    ChangeGlowingState(true);
                    m_ShipGlowing.Update();
                }
                else if (fState.ToString() != "BridgeShips.FSM.VHalfWayPosCorrectState")
                {
                    ChangeGlowingState(false);
                }
            }
            else if (GameEntry.Instance.FsmManager.GetOpenAuxillaryConnection())
            {
                fState = GameEntry.Instance.VirtualRManager.GetFsmStateByShipID(shipID);
                if (fState == null)
                    return;
                if (briShip.IsLinked.Is(true))
                {
                    if (m_aiShip.Settings.Movement.MovementState < ET.AIMovementState.GpsDriving
                        || fState.ToString() == "BridgeShips.FSM.VHalfWayPosCorrectState"
                        || fState.ToString() == "BridgeShips.FSM.VLinkingState")
                    {
                        ChangeGlowingState(false);
                    }
                }
                else if (m_aiShip.Settings.Movement.MovementState == ET.AIMovementState.AIDriving ||
                    fState.ToString() == "BridgeShips.FSM.VHalfWayPosCorrectState" ||
                     fState.ToString() == "BridgeShips.FSM.VPosCorrectState")
                {
                    ChangeGlowingState(true);
                    m_ShipGlowing.Update();
                }
                else if (fState.ToString() == "BridgeShips.FSM.VHalfWayLinkingState"
                    || fState.ToString() == "BridgeShips.FSM.VHalfWayIdleState")
                {
                    ChangeGlowingState(false);
                }
            }
        }
    }
}
